﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ObjectPool
{
    class Program
    {
        static void Main(string[] args)
        {
            var instancePool = new Pool<Instance>(new InstanceCreator(), 1);
            {
                var thread1 = new Thread(() =>
                                            {
                                                Console.WriteLine("Thread #1: started");

                                                Console.WriteLine("Thread #1: Wait object {0}", DateTime.Now.TimeOfDay);
                                                var obj1 = instancePool.Take();
                                                obj1.Property = "asdsad";
                                                Console.WriteLine("Thread #1: Take object {0}", DateTime.Now.TimeOfDay);

                                                Console.WriteLine("Thread #1: Sleep 3000 ms");
                                                Thread.Sleep(3000);

                                                Console.WriteLine("Thread #1: Put object");
                                                instancePool.Put(obj1);
                                            });
                var thread2 = new Thread(() =>
                                            {
                                                Console.WriteLine("Thread #2: started");

                                                Console.WriteLine("Thread #2: Wait object {0}", DateTime.Now.TimeOfDay);
                                                var obj1 = instancePool.Take();
                                                Console.WriteLine("Thread #2: Take object {0}", DateTime.Now.TimeOfDay);

                                                Console.WriteLine("Thread #2: Sleep 2000 ms");
                                                Thread.Sleep(2000);

                                                Console.WriteLine("Thread #2: Put object");
                                                instancePool.Put(obj1);
                                            });

                var thread3 = new Thread(() =>
                                            {
                                                Console.WriteLine("Thread #3: started");

                                                Console.WriteLine("Thread #3: Wait object {0}", DateTime.Now.TimeOfDay);
                                                var obj1 = instancePool.Take();
                                                Console.WriteLine("Thread #3: Take object {0}", DateTime.Now.TimeOfDay);

                                                Console.WriteLine("Thread #3: Sleep 2000 ms");
                                                Thread.Sleep(2000);

                                                Console.WriteLine("Thread #3: Put object");
                                                instancePool.Put(obj1);
                                            });

                var thread4 = new Thread(() =>
                                            {
                                                Console.WriteLine("Thread #4: started");

                                                Console.WriteLine("Thread #4: Wait object {0}", DateTime.Now.TimeOfDay);
                                                var obj1 = instancePool.Take();
                                                Console.WriteLine("Thread #4: Take object {0}", DateTime.Now.TimeOfDay);

                                                Console.WriteLine("Thread #4: Sleep 2000 ms");
                                                Thread.Sleep(2000);

                                                Console.WriteLine("Thread #4: Put object");
                                                instancePool.Put(obj1);
                                            });

                thread1.Start();
                thread2.Start();
                thread3.Start();
                thread4.Start();

                Console.ReadKey();
            }
        }
    }
}
